﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Text.RegularExpressions;
using University.Workshop.DataAccess;
using System.Data;
namespace University.Workshop.Business
{
    public static class ProviderControl
    {
        public static bool RegisterProvider(string address, string contact, string email, string name, string web, string cellular, string nit, string telephone)
        {
            VerifyAttribsProvider(address, contact, email, name, web, cellular, nit, telephone);
            Provider newProvider = new Provider() { Address = address, Cellular = cellular, Contact = contact, Email = email, Name = name, Nit = nit, Telephone = telephone, Web = web };
            return RegisterProvider(newProvider);
        }
        private static void VerifyAttribsProvider(string address, string contact, string email, string name, string web, string cellular, string nit, string telephone)
        {
            #region Verificar Campos Obligatorios
            if (!Regex.IsMatch(nit, @"^\d{5,15}$"))
            {
                throw new BusinessException("Nit es invalido");
            }

            if (!((Regex.IsMatch(name, @"^[A-Za-z0-9 .]*$")) && name.Length < 41 && name.Length > 0))
            {
                throw new BusinessException("Nombre es Invalido");
            }
            if (!((Regex.IsMatch(address, @"^[A-Za-z0-9 .#]*$")) && address.Length < 41 && address.Length > 0))
            {
                throw new BusinessException("Direccion es Invalida");
            }
            if (string.IsNullOrEmpty(telephone) && string.IsNullOrEmpty(cellular))
            {
                throw new BusinessException("Porfavor Ingrese un celular o telefono");
            }
            if ((!string.IsNullOrEmpty(telephone)) && ((telephone.Length != 7) || (!Regex.IsMatch(telephone, "^[0-9]*$"))))
            {
                throw new BusinessException("Telefono es Invalido");
            }
            if ((!string.IsNullOrEmpty(cellular)) && ((cellular.Length != 8) || (!Regex.IsMatch(cellular, "^[0-9]*$"))))
            {
                throw new BusinessException("Celular es Invalido");
            }
            #endregion
            #region Verificar Campos Opcionales
            if ((!string.IsNullOrEmpty(contact)) && ((contact.Length > 41) || (!((Regex.IsMatch(contact, @"^[A-Za-z0-9 .]*$"))))))
            {
                throw new BusinessException("Nombre del Contacto es Invalido");
            }
            if ((!string.IsNullOrEmpty(email)) && ((email.Length > 41) || (!((Regex.IsMatch(email, @"^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$"))))))
            {
                throw new BusinessException("Email es Invalido");
            }
            if ((!string.IsNullOrEmpty(web)) && ((web.Length > 41) || (!((Regex.IsMatch(web, @"^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$"))))))
            {
                throw new BusinessException("Web es Invalido");
            }
            #endregion
        }
        public static bool RegisterProvider(Provider newProvider)
        {
            if (newProvider == null) {
                throw new BusinessException("El proveedor que se quiso ingresar es nulo");
            }
            if (ExistProvider(newProvider.Nit)) {
                throw new BusinessException("El Nit Del Proveedor ya existe", new BusinessException());
            }

            return DataAccessProvider.ProviderRegister(newProvider);
                
        }
        public static bool ExistProvider(string nit)
        {
            return (GetProviderByNit(nit).Nit!=null);
        }
        public static DataTable SearchProvider(string parameter,bool active) {
            DataTable resp = DataAccessProvider.SearchProvider(parameter, active.ToString());

            if (resp.Rows.Count== 0)
            {
                throw new BusinessException("No se obtuvieron resultados");
            }
            else {
                return resp;

            }
        }
        public static Provider GetProviderByNit(string nit) {
            return DataAccessProvider.GetProviderByNit(nit); 
        }
        public static bool UpdateProvider(Provider provider) {
            return DataAccessProvider.UpdateProvider(provider);
        }

        public static void DeleteProviderControl(string nitProvider)
        {
            DataAccessProvider.ProviderDelete(nitProvider);
        }

        public static void EnableProviderControl(string nitProvider)
        {
            DataAccessProvider.EnableProviderData(nitProvider);
        }
    }
}
